package com.css.ids.server.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 系统参数配置表:来自配置文件ids.properties
 */
public final class IdsConfig {
	private java.util.Properties props = new java.util.Properties();
	private static IdsConfig instance = null;
	private static Log log = LogFactory.getLog(IdsConfig.class);

	public static synchronized IdsConfig getConfigurationManager() {
		if (instance == null) {
			instance = new IdsConfig();
			instance.init();
		}
		return instance;
	}

	private void init() {
		// 先装载idsconfig.properties文件
		try {
			props.load(IdsConfig.class.getResourceAsStream("/idsconfig.properties"));
		} catch (Exception e) {
			log.error("装载系统配置文件【idsconfig.properties】失败", e);
		}
	}

	/**
	 * 获取配置参数
	 */
	public String getString(String key) {
		return props.getProperty(key);
	}

	public String getString(String key, String defaultValue) {
		return props.getProperty(key, defaultValue);
	}

	public int getInt(String key, int defaultValue) {
		String cfg = props.getProperty(key);
		if (cfg == null)
			return defaultValue;
		else {
			return Integer.parseInt(cfg.trim());
		}
	}

	public boolean getBool(String key, boolean dv) {
		String cfg = props.getProperty(key);
		if (cfg == null)
			return dv;
		else {
			return Boolean.parseBoolean(cfg.trim().toLowerCase());
		}
	}

	public void remove(String key) {
		props.remove(key);
	}

	public void update(String key, String value) {
		props.setProperty(key, value);
	}

	/**
	 * 获取IdsServiceUrl
	 */
	public static String getIdsServiceUrl() {
		return IdsConfig.getConfigurationManager().getString("IdsServiceUrl");
	}
}